<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幸运刮刮卡</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial Rounded MT Bold', 'Arial', sans-serif;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            color: #fff;
        }
        
        .container {
            max-width: 800px;
            width: 100%;
            text-align: center;
        }
        
        h1 {
            font-size: 2.8rem;
            margin-bottom: 10px;
            text-shadow: 0 3px 6px rgba(0,0,0,0.3);
            letter-spacing: 1px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            margin-bottom: 30px;
            opacity: 0.9;
        }
        
        .scratch-container {
            position: relative;
            width: 100%;
            max-width: 500px;
            margin: 0 auto 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
            border-radius: 20px;
            overflow: hidden;
        }
        
        .scratch-card {
            position: relative;
            width: 100%;
            height: 300px;
            background: linear-gradient(45deg, #ff9966, #ff5e62);
            border-radius: 20px;
            overflow: hidden;
        }
        
        .result-area {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            background: linear-gradient(135deg, #43cea2, #185a9d);
            z-index: 1;
        }
        
        .result-text {
            font-size: 2.5rem;
            font-weight: bold;
            margin-bottom: 15px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }
        
        .prize-amount {
            font-size: 3.5rem;
            font-weight: 900;
            color: #FFD700;
            text-shadow: 0 0 10px rgba(255, 215, 0, 0.7);
            margin-bottom: 20px;
        }
        
        .congrats {
            font-size: 1.5rem;
            margin-bottom: 25px;
            max-width: 80%;
            line-height: 1.5;
        }
        
        .scratch-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: repeating-linear-gradient(
                45deg,
                #d9d9d9,
                #d9d9d9 10px,
                #c0c0c0 10px,
                #c0c0c0 20px
            );
            cursor: grabbing;
            z-index: 2;
        }
        
        .scratch-canvas {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 3;
        }
        
        .instructions {
            font-size: 1.1rem;
            margin-top: 20px;
            background: rgba(255,255,255,0.1);
            padding: 15px;
            border-radius: 10px;
            max-width: 500px;
            margin: 0 auto 30px;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 20px;
            flex-wrap: wrap;
        }
        
        button {
            background: #FFD700;
            color: #333;
            border: none;
            padding: 15px 30px;
            font-size: 1.1rem;
            font-weight: bold;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }
        
        button:hover {
            background: #ffcc00;
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.3);
        }
        
        button:active {
            transform: translateY(1px);
        }
        
        .stats {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin-top: 30px;
            background: rgba(255,255,255,0.1);
            padding: 15px 30px;
            border-radius: 10px;
            width: 100%;
            max-width: 500px;
        }
        
        .stat-item {
            text-align: center;
        }
        
        .stat-value {
            font-size: 2rem;
            font-weight: bold;
            color: #FFD700;
        }
        
        .stat-label {
            font-size: 0.9rem;
            opacity: 0.8;
        }
        
        .footer {
            margin-top: 40px;
            font-size: 0.9rem;
            opacity: 0.7;
        }
        
        @media (max-width: 600px) {
            h1 {
                font-size: 2.2rem;
            }
            
            .scratch-card {
                height: 250px;
            }
            
            .result-text {
                font-size: 2rem;
            }
            
            .prize-amount {
                font-size: 2.8rem;
            }
            
            .stats {
                flex-direction: column;
                gap: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>幸运刮刮卡</h1>
        <p class="subtitle">刮开涂层，试试你的手气！</p>
        
        <div class="scratch-container">
            <div class="scratch-card">
                <div class="result-area">
                    <div class="result-text">恭喜您获得</div>
                    <div class="prize-amount">¥50</div>
                    <div class="congrats">幸运大奖！奖金已存入您的账户</div>
                </div>
                <div class="scratch-overlay"></div>
                <canvas id="scratchCanvas" class="scratch-canvas"></canvas>
            </div>
        </div>
        
        <div class="instructions">
            按住鼠标左键并移动来刮开涂层，刮开面积超过70%即可查看结果
        </div>
        
        <div class="controls">
            <button id="newCardBtn">再来一张</button>
            <button id="resetBtn">重新刮开</button>
        </div>
        
        <div class="stats">
            <div class="stat-item">
                <div class="stat-value" id="scratchCount">0</div>
                <div class="stat-label">已刮卡片</div>
            </div>
            <div class="stat-item">
                <div class="stat-value" id="totalWinnings">¥0</div>
                <div class="stat-label">累计奖金</div>
            </div>
        </div>
        
        <div class="footer">
            © 2023 幸运刮刮卡 | 本游戏仅供娱乐
        </div>
    </div>

    <audio id="scratchSound" preload="auto">
        <source src="https://assets.mixkit.co/sfx/preview/mixkit-scribbling-on-paper-260.mp3" type="audio/mpeg">
    </audio>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取Canvas元素和上下文
            const canvas = document.getElementById('scratchCanvas');
            const ctx = canvas.getContext('2d');
            
            // 获取其他DOM元素
            const scratchOverlay = document.querySelector('.scratch-overlay');
            const newCardBtn = document.getElementById('newCardBtn');
            const resetBtn = document.getElementById('resetBtn');
            const scratchSound = document.getElementById('scratchSound');
            const scratchCountEl = document.getElementById('scratchCount');
            const totalWinningsEl = document.getElementById('totalWinnings');
            
            // 初始化变量
            let isDrawing = false;
            let scratchCount = 0;
            let totalWinnings = 0;
            let currentPrize = 0;
            
            // 设置Canvas尺寸
            function initCanvas() {
                const container = canvas.parentElement;
                canvas.width = container.offsetWidth;
                canvas.height = container.offsetHeight;
                
                // 初始化Canvas为不透明灰色
                ctx.fillStyle = '#a0a0a0';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                ctx.globalCompositeOperation = 'destination-out';
            }
            
            // 初始化刮刮卡
            function initScratchCard() {
                initCanvas();
                scratchOverlay.style.display = 'block';
                
                // 随机生成奖金 (50%概率中奖)
                const isWinner = Math.random() > 0.5;
                currentPrize = isWinner ? Math.floor(Math.random() * 91) + 10 : 0; // 10-100元或0元
                
                // 更新结果区域
                const resultText = document.querySelector('.result-text');
                const prizeAmount = document.querySelector('.prize-amount');
                const congrats = document.querySelector('.congrats');
                
                if (currentPrize > 0) {
                    resultText.textContent = '恭喜您获得';
                    prizeAmount.textContent = `¥${currentPrize}`;
                    prizeAmount.style.color = '#FFD700';
                    congrats.textContent = '幸运大奖！奖金已存入您的账户';
                } else {
                    resultText.textContent = '很遗憾';
                    prizeAmount.textContent = '未中奖';
                    prizeAmount.style.color = '#ccc';
                    congrats.textContent = '再接再厉，下次好运！';
                }
            }
            
            // 开始刮卡
            function startScratch(e) {
                isDrawing = true;
                scratchSound.currentTime = 0;
                scratchSound.play();
                draw(e);
            }
            
            // 结束刮卡
            function stopScratch() {
                isDrawing = false;
                scratchSound.pause();
                
                // 检查刮开面积
                checkScratchProgress();
            }
            
            // 绘制刮痕
            function draw(e) {
                if (!isDrawing) return;
                
                const rect = canvas.getBoundingClientRect();
                const x = e.clientX - rect.left;
                const y = e.clientY - rect.top;
                
                ctx.beginPath();
                ctx.arc(x, y, 20, 0, Math.PI * 2);
                ctx.fill();
                
                // 随机播放刮擦音效
                if (Math.random() > 0.7) {
                    scratchSound.currentTime = 0;
                    scratchSound.play();
                }
            }
            
            // 检查刮开进度
            function checkScratchProgress() {
                const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                const pixels = imageData.data;
                let transparentCount = 0;
                
                // 计算透明像素数量
                for (let i = 3; i < pixels.length; i += 4) {
                    if (pixels[i] === 0) {
                        transparentCount++;
                    }
                }
                
                // 计算透明像素比例
                const totalPixels = (canvas.width * canvas.height);
                const percentScratched = (transparentCount / totalPixels) * 100;
                
                // 如果刮开超过70%，显示结果
                if (percentScratched > 70) {
                    scratchOverlay.style.display = 'none';
                    
                    // 更新统计信息
                    scratchCount++;
                    scratchCountEl.textContent = scratchCount;
                    
                    if (currentPrize > 0) {
                        totalWinnings += currentPrize;
                        totalWinningsEl.textContent = `¥${totalWinnings}`;
                    }
                }
            }
            
            // 重置刮刮卡
            function resetScratchCard() {
                initCanvas();
                scratchOverlay.style.display = 'block';
            }
            
            // 生成新刮刮卡
            function generateNewCard() {
                scratchCount++;
                scratchCountEl.textContent = scratchCount;
                initScratchCard();
            }
            
            // 事件监听
            canvas.addEventListener('mousedown', startScratch);
            canvas.addEventListener('mousemove', draw);
            canvas.addEventListener('mouseup', stopScratch);
            canvas.addEventListener('mouseleave', stopScratch);
            
            // 触摸事件支持
            canvas.addEventListener('touchstart', (e) => {
                e.preventDefault();
                startScratch(e.touches[0]);
            });
            
            canvas.addEventListener('touchmove', (e) => {
                e.preventDefault();
                draw(e.touches[0]);
            });
            
            canvas.addEventListener('touchend', stopScratch);
            
            resetBtn.addEventListener('click', resetScratchCard);
            newCardBtn.addEventListener('click', generateNewCard);
            
            // 初始化
            initScratchCard();
        });
    </script>
</body>
</html>